import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;


public class SharedObject {

	
	//Refernec to object such as Client. Software (virutal device), Adrino cients (pyhsica device)
	ArrayList<ClientsForSharedObject> clients = new ArrayList<ClientsForSharedObject>();
	ArrayList<ClientsForSharedObject> softwareClients = new ArrayList<ClientsForSharedObject>();
	ArrayList<ClientsForSharedObject> ardClients = new ArrayList<ClientsForSharedObject>();
	
	
	// Remove Client from clients(arraylist)
	public void removeClient(int clientID) {
		for (int i = clients.size()-1; i >= 0; i--) {
			if (clients.get(i).getID() == clientID) {
				clients.remove(i);
				System.out.println("[SharedObject] : Client Removed.");
				break;
			}			
		}
		System.out.println("[SharedObject] : No Clients Found.");
	}
	
	// Add client to SharedClients (arraylist)
	public void addClient(Socket clientSocket, int clientID) {
		clients.add(new ClientsForSharedObject(clientSocket, clientID));
		System.out.println("[SharedObject] : Client Added.");
	}
	
	//Add arduino clients to ardClients (arraylist)
	public void addArdClient(Socket clientSocket, int clientID){
		ardClients.add(new ClientsForSharedObject(clientSocket, clientID));
		System.out.println("[SharedObject] : Special client added.");
	}
	
	//Add software clients to softwareClients (arraylist)
	public void addSoftwareClient(Socket clientSocket, int clientID){
		softwareClients.add(new ClientsForSharedObject(clientSocket, clientID));
		System.out.println("[SharedObject] : Special software client added.");
	}
	
	// Send information to a  specifik client. // not yet fully implemented.
	public void sendInformationSpecClient(int clientID, String info)
	{
		for (int i = 0; i < clients.size(); i++) {
			if(clients.get(i).getID() == clientID){
				try {
					Socket socket = clients.get(i).getSocket();
					PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
					
					pw.println(info); // syntax not implemented.
				} catch(Exception e) {
					System.out.println("[SharedObject] : Error " + e);
				}
			}
		}
	}
	
	
	// Send information to arduino
	public void sendInformationArd(int clientID, String info){
		for (int i = 0; i < ardClients.size(); i++){
			try {
				Socket socket = ardClients.get(i).getSocket();
				PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
				pw.println(info);
			} catch (IOException e) {
				System.out.println("[SharedObject] : Error " + e);
			}
		}

	}
		
	
	// Getting ardinou socket and then  send information to it and recives an answer and returns it to the spesific client
	public void sendInformationArdThenClient(Socket clientSocket,int clientID, String info){
		for (int i = 0; i < ardClients.size(); i++){
			try {
				Socket ardSocket = ardClients.get(i).getSocket();
				PrintWriter ardPrintWriter = new PrintWriter(ardSocket.getOutputStream(), true);
				ardPrintWriter.println(info);

				BufferedReader ardBufferedReader = new BufferedReader(new InputStreamReader(ardSocket.getInputStream()));
				String msg =ardBufferedReader.readLine();

				PrintWriter clientPrintWriter = new PrintWriter(clientSocket.getOutputStream(), true);
				clientPrintWriter.println(msg);
				
				for(int u=0; u < 100000000; u++)
				{
					// Time loop
				}
				
				ClientHandler.temp=false;
			} catch (IOException e) {
				System.out.println("[SharedObject] : Error " + e);
			}
		}
	}
	
	
	// Send information from a client to software debice (virtual)
	public void sendInformationSoftware(int clientID, String info){
		for (int i = 0; i < softwareClients.size(); i++){
			try {
				Socket softwareSocket = softwareClients.get(i).getSocket();
				PrintWriter softwarePrintWriter = new PrintWriter(softwareSocket.getOutputStream(), true);
				
				softwarePrintWriter.println(info);
				BufferedReader softwareBufferedReader = new BufferedReader(new InputStreamReader(softwareSocket.getInputStream()));
				
				softwareBufferedReader.readLine();
			
			} catch (IOException e) {
				System.out.println("[SharedObject] : Error " + e);
			}
		}
	}
	

	// Returns all client that is connected
	public String getClientIp(){
		String usersIP="users  ";
		for (int i = 0; i < clients.size(); i++) {
				try {
					usersIP+=""+clients.get(i).getSocket().getInetAddress()+" : ";
				} catch(Exception e) {
					System.out.println("[SharedObject] : Error " + e);
				}
		}
		return usersIP;
	}
		
	// Sends information to a virtual device (software)
	public void sendInformationToSoftwareThenClient(Socket clientSocket,int clientID, String info){
		for (int i = 0; i < softwareClients .size(); i++){
			try {
				Socket softwareSocket = softwareClients .get(i).getSocket();
				PrintWriter softwarePrintWriter = new PrintWriter(softwareSocket.getOutputStream(), true);
				
				softwarePrintWriter.println(info);

				for(int u=0; u < 100000000; u++){
					//time loop
				}
				
				
				BufferedReader softwareBufferedReader = new BufferedReader(new InputStreamReader(softwareSocket.getInputStream()));
				String msgFromSoftware = softwareBufferedReader.readLine();
				
				PrintWriter clientPrintWriter = new PrintWriter(clientSocket.getOutputStream(), true);
				clientPrintWriter.println(msgFromSoftware);
				
				for(int u=0; u < 100000000; u++)
				{
					//time loop
				}

			} catch (IOException e) {
				System.out.println("[SharedObject] : Error " + e);
			}
		}
	}
		
	
	
	// Sends what device that should be removed or added to the software (virutal device)
	public void addRemoveSoftwareDevice(Socket clientSocket,int clientID, String info){
		for (int i = 0; i < softwareClients .size(); i++){
			try {
				Socket socket = softwareClients .get(i).getSocket();
				PrintWriter softwareSocket = new PrintWriter(socket.getOutputStream(), true);
				BufferedReader softwareBufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				softwareSocket.println(info);

				String msg = softwareBufferedReader.readLine();
				
				
				PrintWriter clientPrintWriter = new PrintWriter(clientSocket.getOutputStream(), true);
				clientPrintWriter.println(msg);
				for(int u=0; u < 100000000; u++)
				{
					//time loop
				}

			} catch (IOException e) {
				System.out.println("[SharedObject] : Error " + e);
			}
		}
	}	
}
